from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from _db import Db
import json
from copy import deepcopy
import requests
from bs4 import BeautifulSoup
import re
from pyppeteer import launch
import asyncio
# from func_timeout import func_set_timeout
import time
import threading
import random
from tkinter import Tk
import os
from bs4 import BeautifulSoup
import copy

async def login(index):
    accountlist=[
        # ['直觉一号','zhijue001'],
        # ['直觉二号', 'zhijue001'],
        # ['直觉三号', 'zhijue001'],
        ['19921034519','zhijue001']
        # ['十年九夏那么伤959', 'Zhijue000']
    ]
    if index==len(accountlist)-1:
        index=0
    username=accountlist[index][0]
    pwd=accountlist[index][1]

    print('username:'+username)
    print('pwd:'+pwd)

    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    proxy = '12.20.241.112:80'
    # headless参数设为False，则变成有头模式
    browser = await launch({
                            # 'args': ['--proxy-server={}'.format(proxy), ],
                            'headless': False,
                            'slowMo': 10, 'devtools': False,
                            'executablePath': 'C:\Program Files (x86)\chrome-win/chrome.exe',
                            # "args": ["--start-maximized"]
                            'dumpio': True,
                            })
    page = await browser.newPage()
    await page.setViewport({'width': 1366, 'height': 768})
    # await page.setUserAgent(
    #     'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36')

    # await page.authenticate({
    #     'username': '',
    #     'password': ''
    # })
    # await page.goto("https://www.baidu.com")
    # time.sleep(1000)
    await page.goto('https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fwww.taobao.com%2F')
    await page.waitFor(1000)

    await page.evaluate(
        '''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')  # 以下为插入中间js，将淘宝会为了检测浏览器而调用的js修改其结果。
    await page.evaluate('''() =>{ window.navigator.chrome = { runtime: {},  }; }''')
    await page.evaluate(
        '''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
    await page.evaluate(
        '''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
    await page.type('#fm-login-id', username, {'delay':random.randint(50,100)})
    await page.type('#fm-login-password', pwd, {'delay': random.randint(50,100)})
    await page.waitFor(1000)
    await page.click('#login-form > div.fm-btn > button')
    print("正常登陆")
    await page.waitFor(5000)
    cookie = await page.cookies()
    return cookie




loop = asyncio.get_event_loop()
cookie = loop.run_until_complete(login(0))
